package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.user.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.swing.plaf.ProgressBarUI;

/**
 * @author: itheima
 * @create: 2022-05-21 15:31
 */
@Component
@Slf4j
public class FollowBeahaviorListener {

    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    /**
     * 监听到用户关注行为记录，将行为入库
     * @param record
     */
    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handeMsg(ConsumerRecord<String, String> record){
        if(StringUtils.isNotBlank(record.value())){
            FollowBehaviorDto followBehaviorDto = JSON.parseObject(record.value(), FollowBehaviorDto.class);
            followBehaviorService.saveFollowBehavior(followBehaviorDto);
        }
    }
}
